# test.py
import time

import requests
import base64

# 测试用基础配置
URL = "http://127.0.0.1:5000/api/ocr/recognize"  # 确保服务已运行
TEST_IMAGE_PATH = "/root/autodl-tmp/test.png"


def test_ocr_api():
    # 1. 读取测试图片并编码为base64
    with open(TEST_IMAGE_PATH, "rb") as f:
        start_time = time.time()
        img_base64 = base64.b64encode(f.read()).decode("utf-8")

    # 2. 构造请求数据
    payload = {"img_base64": img_base64}

    try:
        # 3. 发送POST请求
        response = requests.post(URL, json=payload)

        # 4. 检查响应状态码
        if response.status_code == 200:
            print("✅ 测试成功！响应数据：", response.json())
            end_time = time.time()
            print(f"✅ 测试成功！响应时间：{end_time - start_time}秒")
        else:
            print(f"❌ 测试失败，状态码：{response.status_code}，响应内容：{response.text}")

    except Exception as e:
        print(f"⚠️  请求异常：{str(e)}")


if __name__ == "__main__":
    test_ocr_api()